# -*- coding: utf-8 -*-
"""
Created on Tue Aug 16 09:47:47 2022

@author: wulong
"""
import numpy as np
from casadi import *
from Basic_paras import *

# In[1] Fast EMPC 1 model
# States
def ode_ies_f1(x_f1, xs, x_f2, x_f3, us, u_f1, u_f2, u_f3, uz, ud):
    dxdt = SX.zeros(Nx_f1)
    
    C_soc = xs[0]
    C_sot = xs[1]
    C_stc = xs[2]
    C_sth = xs[3]
    tbr = xs[4]
    
    If = x_f1[0]
    Gh2 = x_f1[1]
    pO2 = x_f1[2]
    pH2O = x_f1[3]
    pH2 = x_f1[4]
    vcap = x_f1[5]
    Iba = x_f1[6]
    
    Pmtf = x_f2[0]
    tabf = x_f2[1]
    tabw = x_f2[2]
    tabt = x_f2[3]
    tewm = x_f2[4]
    tre = x_f2[5]
    
    tc = x_f3[0]
    tcs = x_f3[1]
    tcwm = x_f3[2]
    te = x_f3[3]
    tes = x_f3[4]
    
    Gab = us[0]
    Gec = us[1]
    Gstu = us[2]
    
    Gff = u_f1[0]
    Pbar = u_f1[1]
    
    Gfm = u_f2
    
    Nec = u_f3
    
    zfc = uz[0]
    zma = uz[1]
    zec = uz[2]
    zst = uz[3]
    
    ta = ud[0]
    Sra = ud[1]
    Pd = ud[2]
    Qother = ud[3]
    
    # FC
    Ifc = fu_d/(2*Kr)*Gh2
    Gff_mol = Gff*Mch4
    Go2 = 1/r_HO*Gh2
    Gh2r = 2*Kr*If
    
    # BA
    iba = Iba/npb
    Vba = nsb*(Em - vcap - R0b*iba)
    Iba_cal = Pbar*1000/Vba
    
    dxdt[0] = zfc*((Ifc - If)/tau_e)
    dxdt[1] = zfc*((Gff_mol - Gh2)/tau_f)
    dxdt[2] = zfc*((1/KO2*(Go2 - If*Kr) - pO2)/tau_O2)
    dxdt[3] = zfc*((1/KH2O*Gh2r - pH2O)/tau_H2O)
    dxdt[4] = zfc*((1/KH2*(Gh2- Gh2r) - pH2)/tau_H2)
    dxdt[5] = iba/C1b - vcap/(R1b*C1b)
    dxdt[6] = (Iba_cal - Iba)/tau_dc
    
    return dxdt

# Output
def out_ies_f1(x_f1, xs, x_f2, x_f3, us, u_f1, u_f2, u_f3, uz, ud):
    y_alg = MX.zeros(Ny_f1)
    
    C_soc = xs[0]
    C_sot = xs[1]
    C_stc = xs[2]
    C_sth = xs[3]
    tbr = xs[4]
    
    If = x_f1[0]
    Gh2 = x_f1[1]
    pO2 = x_f1[2]
    pH2O = x_f1[3]
    pH2 = x_f1[4]
    vcap = x_f1[5]
    Iba = x_f1[6]
    
    Pmtf = x_f2[0]
    tabf = x_f2[1]
    tabw = x_f2[2]
    tabt = x_f2[3]
    tewm = x_f2[4]
    tre = x_f2[5]
    
    tc = x_f3[0]
    tcs = x_f3[1]
    tcwm = x_f3[2]
    te = x_f3[3]
    tes = x_f3[4]
    
    Gab = us[0]
    Gec = us[1]
    Gstu = us[2]
    
    Gff = u_f1[0]
    Pbar = u_f1[1]
    
    Gfm = u_f2
    
    Nec = u_f3
    
    zfc = uz[0]
    zma = uz[1]
    zec = uz[2]
    zst = uz[3]
    
    ta = ud[0]
    Sra = ud[1]
    Pd = ud[2]
    Qother = ud[3]
    
    # PV
    I_pv_max = I_max_0*Sra/Sra_pv_0*(1 + c_pv1*(ta - T_pv_0))
    V_pv_max = V_max_0*log(exp(1) + c_pv2*(Sra - Sra_pv_0))*(1 - c_pv3*(ta - T_pv_0))
    Ppv = npp*nsp*I_pv_max*V_pv_max/1000
    
    # FC
    eta_a = afc + bfc*log(If + eps)
    eta_c = -R0fc*T0fc/(2*F0)*log(1-If/IL + eps)
    eta_o = If*rfc
    V0 = N0fc*(E0fc + R0fc*T0fc/(2*F0)*log(pH2*sqrt(pO2)/(pH2O + eps) + eps))
    Vfc = V0 - eta_a - eta_c - eta_o
    Ifc = fu_d/(2*Kr)*Gh2
    Pfc = zfc*(Vfc*Ifc/1000)
    
    # PIP in the rerurn side
    Gall = Gab + Gec + Gstu
        
    # MA
    Pmt = zma*(Pmt0 + Pmtf)
    
    # EC
    pc = exp(21.3-2025.5/(248.94+tc))/1e6
    pe = exp(21.3-2025.5/(248.94+te))/1e6
    pr = pc/pe # Compressor
    etavl = 0.98-0.085*(pr**(1/kr)-1)
    etacp = 0.9085*exp(-0.06443*pr)-7.605*exp(-3.155*pr)
    Gr = etavl*Nec*roeg*Vcp
    wi = kr/(1e3*(kr-1))*pe*1e6/roeg*(pr**((kr-1)/kr)-1)
    Pcp = zec*(Gr*wi/etacp)
    
    # BR
    Hpmp = Spmp*Gall**2/(rhow*ge)
    eta_pmp = b0p*Gall**2 + b1p*Gall + b2p
    Ppmp = Gall*ge*Hpmp/(1e3*eta_pmp)
    
    # BA
    iba = Iba/npb
    Vba = nsb*(Em - vcap - R0b*iba)
    Pba = Vba*Iba/1000
    
    # Electric Power
    Psc = Pcp + Ppmp
    Psl = Ppv + Pfc + Pmt + Pba - Psc - Pd
    
    y_alg = Psl
    
    return y_alg

# In[2] Formulate discrete time dynamics {x_f1, xs, x_f2, x_f3, us, u_f1, u_f2, u_f3, uz, ud}
x_sym_f1 = SX.sym('x_f1', Nx_f1)
x_s_sym_f1 = SX.sym('x_s_f1', Nx_s)
x_f2_sym_f1 = SX.sym('x_f2_f1', Nx_f2)
x_f3_sym_f1 = SX.sym('x_f3_f1', Nx_f3)

u_s_sym_f1 = SX.sym('u_s_f1', Nuc_s)
u_sym_f1 = SX.sym('u_f1', Nuc_f1)
u_f2_sym_f1 = SX.sym('u_f2_f1', Nuc_f2)
u_f3_sym_f1 = SX.sym('u_f3_f1', Nuc_f3)

uz_sym_f1 = SX.sym('uz_f1', Nuz_h)
ud_sym_f1 = SX.sym('ud_f1', Nud_f1)

ode_sym_f1 = ode_ies_f1(x_sym_f1, x_s_sym_f1, x_f2_sym_f1, x_f3_sym_f1, 
                        u_s_sym_f1, u_sym_f1, u_f2_sym_f1, u_f3_sym_f1, 
                        uz_sym_f1, ud_sym_f1)

args_f1 = {'x': x_sym_f1, 'p': vertcat(x_s_sym_f1, x_f2_sym_f1, x_f3_sym_f1, 
                                       u_s_sym_f1, u_sym_f1, u_f2_sym_f1, u_f3_sym_f1, 
                                       uz_sym_f1, ud_sym_f1), 'ode': ode_sym_f1}

opts_f1 = {'tf': Delta_f, 'regularity_check': True}
I_ode_f1 = integrator('I_ode_f1', 'rk', args_f1, opts_f1)

